{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cleanup\n",
    "This notebook is provided to help you clean up any resources you have created by running through the example. You should also go to the [CloudFormation console](https://console.aws.amazon.com/cloudformation/home) and delete the stack that you created."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sagemaker import get_execution_role\n",
    "import sagemaker\n",
    "import boto3\n",
    "import json\n",
    "import sys\n",
    "\n",
    "role = get_execution_role()\n",
    "sm = boto3.Session().client(service_name='sagemaker')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Delete feature groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    sm.delete_feature_group(FeatureGroupName='cc-agg-batch-fg') \n",
    "    print('deleted batch fg')\n",
    "except:\n",
    "    pass\n",
    "\n",
    "try:\n",
    "    sm.delete_feature_group(FeatureGroupName='cc-agg-fg') # use if needed to re-create\n",
    "    print('deleted fg')\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sm.list_feature_groups()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stop the KDA SQL App"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "kda_client = boto3.client('kinesisanalytics')\n",
    "\n",
    "try:\n",
    "    kda_client.stop_application(ApplicationName='cc-agg-app')\n",
    "except:\n",
    "    pass\n",
    "\n",
    "print('Stopped the KDA SQL app')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Delete the KDA SQL App"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "try:\n",
    "    ready = False\n",
    "    while not ready:\n",
    "        app_desc = kda_client.describe_application(ApplicationName='cc-agg-app')['ApplicationDetail']\n",
    "        if app_desc['ApplicationStatus'] == 'READY':\n",
    "            ready = True\n",
    "        else:\n",
    "            print('Waiting for KDA SQL app to be ready for deletion...')\n",
    "            time.sleep(15)\n",
    "    create_timestamp = app_desc['CreateTimestamp']\n",
    "    response = kda_client.delete_application(ApplicationName='cc-agg-app',\n",
    "                                  CreateTimestamp=create_timestamp)\n",
    "    print('Deleted KDA SQL app')\n",
    "except:\n",
    "    print('FAILED to delete KDA sql app')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Remove the trigger from Lambda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "\n",
    "%store -r \n",
    "\n",
    "lambda_client = boto3.client('lambda')\n",
    "paginator = lambda_client.get_paginator('list_event_source_mappings')\n",
    "mapping_iterator = paginator.paginate(FunctionName=lambda_to_model_arn)\n",
    "\n",
    "for m in mapping_iterator:\n",
    "    if len(m['EventSourceMappings']) > 0:\n",
    "        uuid = m['EventSourceMappings'][0]['UUID']\n",
    "        print(f'Deleting mapping: {uuid}...')\n",
    "        lambda_client.delete_event_source_mapping(UUID=uuid)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Delete the Kinesis data stream"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kinesis_client = boto3.client('kinesis')\n",
    "try:\n",
    "    kinesis_client.delete_stream(StreamName='cc-stream')\n",
    "except:\n",
    "    pass\n",
    "print('deleted Kinesis stream')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Delete the SageMaker endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%store -r\n",
    "try:\n",
    "    sm.delete_endpoint(EndpointName=endpoint_name)\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
